// page/index/index.js

const { get } = require("../../util/request")

// const request =require('../../util/request')
Page({

    /**
     * 页面的初始数据
     */
    data: {
      baseURL:wx.baseURL,
      inputValue:'',//搜索的内容


      //轮播图
      movies: [],
     indicatorDots: true,  //是否显示面板指示点
     autoplay: true,      //是否自动切换
     interval: 3000,       //自动切换时间间隔
     duration: 1000,       //滑动动画时长
     inputShowed: false,
     loop:true,
     inputVal: "",
 
//小菜单
menu:[],
//商品详情
product:[],
//新闻
news: [],
newsIndex:0,
newsTimer:0,
newIndes:'transform:translate(0rpx,0rpx)',
pageNum:1,
total:0,
timer:0,
loading:"数据加载中..."

    
    },

  //新闻
    getnews(){//获取新闻
      return new Promise((resolve,reject)=>{
        wx.get('/index/news').then((res) =>{
         
             this.setData({
               news:res
             })
         this.changeNewsIndex()//触发新闻轮播
         resolve()
       })
      })
      
    },

  //修改新闻轮播
      changeNewsIndex(){
        if(this.data.newsTimer){
          clearInterval(this.data.newsTimer)
          this.data.newsTimer = 0
        }
        this.data.newsTimer = setInterval(() =>{
          let newsIndex = this.data.newsIndex
          newsIndex ++
          this.setData({
            newsIndex,
            newIndes:'transform:translate(0rpx,-'+newsIndex*80+'rpx);transition:transform 0.3s ease;'
          })
          if(this.data.newsIndex==2){
            setTimeout(()=>{
              this.setData({
                newsIndex:0,
                newIndes:'transform:translate(0rpx,0rpx);'
              })
            },500)
          }
        },3000)
      },

//获取轮播图
    getmovies(){//获取轮播图
      return new Promise((resolve,reject)=>{
        wx.get('/index/banners').then((res)=> {
          this.setData({
            movies:res
          })
          resolve()
        })
        })
   
    },

    //获取分类小菜单
    getmenu(){//获取小菜单分类区数据
      return new Promise((resolve,reject)=>{
        wx.get('/index/categorys').then((res)=>{
          this.setData({
            menu:res
          })
          resolve()
        })
      })
 
  },

//下拉刷新
    onPullDownRefresh: function () {
      this.data.pageNum =1
      this.data.product=[]
      // Promise.all([ this.getnews(),this.getmovies(),this.getmenu(), this.getproduct()]).then(()=>{
      //   wx.stopPullDownRefresh()
      // }).catch(e=>{
      //  wx.stopPullDownRefresh()
      // })
      this.getList()
     
     

    },



//上拉加载
    onReachBottom(){
      if(this.data.total > this.data.product.length){
        this.data.pageNum ++
        this.getproduct()
      }
    },
  //获取商品分页
    getproduct(){//获取商品分页推荐数据
      return new Promise((resolve,reject)=>{
        this.setData({loading:"数据加载中..."})
        wx.get('/index/recommends',{
          pageNum:this.data.pageNum
        }).then( res  =>{
          this.setData({
            loading:"小主，已加载完成哦~",
            product:this.data.product.concat(res.rows),
            total:res.total
          })
        resolve()
        })
      })
     
    },

    click(callback){
      console.log(e);
      var name=e.currentTarget.dataset.name;
      console.log('点击姓名'+name)
      
          },

//轮播图跳转事件
    swiperClick(){
      let path=this.data.banners.path
      wx.swiperClick({
        url:this.path
      })
    },
    
    
    tapName(event){
      let path=event.currentTarget.dataset.path
  console.log('点击成功',event)
  wx.showModal({
    cancelColor: 'cancelColor',
    content:'是否跳转到'+path+'页面',
    title:'提示',
    success(e){
      console.log(path)
      wx.switchTab({
        url: '../classify/classify'
      })
    }
  })
},
    /**
     * 生命周期函数--监听页面加载
     */
    getList(){
      Promise.all([ this.getnews(),this.getmovies(),this.getmenu(), this.getproduct()]).then(()=>{
        console.log("所有数据加载完成")
      }).catch(e=>{
        console.log(e);
      })
    },

onLoad() {
this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
   
    

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})